// Copyright (c) 2013, Webit Team. All Rights Reserved.
package cn.iver.ext.webit.script;

import com.jfinal.kit.StringKit;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author zqq90 <zqq_90@163.com>
 */
public class MethodUtil {

    //TODO: SimpleDateFormat 是非线程安全的
    private final static SimpleDateFormat TIME_STAMP_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat MY_DATE_FORMAT = new SimpleDateFormat("yyyy年MM月dd日");

    public Object isSame(String one, String two, Object three) {
        if (one != null && two != null) {
            if (StringKit.notBlank(one, two) && one.equals(two)) {
                return three;
            }
        }
        return null;
    }

    public static String getNiceDate(Object o) {
        if (null == o) {
            return "";
        }
        String dateString = o.toString();
        String result = null;
        try {
            Date date = TIME_STAMP_FORMAT.parse(dateString);
            long currentTime = new Date().getTime() - date.getTime();
            int time = (int) (currentTime / 1000);
            if (time < 60) {
                result = "刚刚";
            } else if (time >= 60 && time < 3600) {
                result = time / 60 + "分钟前";
            } else if (time >= 3600 && time < 86400) {
                result = time / 3600 + "小时前";
            } else if (time >= 86400 && time < 864000) {
                result = time / 86400 + "天前";
            } else {
                result = MY_DATE_FORMAT.format(date);
            }
        } catch (Exception ignore) {
            //TODO:
        }
        return result;
    }

}
